package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-05-18
 */
@RestController

@RequestMapping("/admin/edu/course")
public class AdminCourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    CourseDescriptionService courseDescriptionService;
    //6、发布课程
    @PutMapping("course-publish/{courseId}")
    public R coursePublish(@PathVariable String courseId){
        /*
        update table set col1=xx , col2=xxx where xxxx=xxx
         */
        LambdaUpdateWrapper<Course> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Course::getPublishTime , new Date());
        updateWrapper.set(Course::getStatus , "Normal");
        updateWrapper.eq(Course::getId , courseId);
        courseService.update(updateWrapper);
        return R.ok();
    }
    //5、发布课程：回显课程信息
    @GetMapping("get-course-publish/{courseId}")
    public R getCoursePublish(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getCoursePublish(courseId);
        return R.ok().setData("item" , coursePublishVo);
    }
    //4、查询课程列表
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable Integer pageNum , @PathVariable Integer pageSize ){
        Page<CourseInfoVo> page = new Page<>(pageNum , pageSize);//数据需要多表联查
        LambdaQueryWrapper<CourseInfoVo> queryWrapper = new LambdaQueryWrapper<>();
        page = courseService.pageCourseInfoVo(page,queryWrapper);
        return R.ok().setData("page" , page);
    }



    //2、查询回显课程基本信息
    @GetMapping("get-course-info/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        // id是主键，主键默认会创建主键索引
        Course course = courseService.getById(courseId);
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course , courseInfoForm);
        courseInfoForm.setDescription(courseDescription.getDescription());
        return R.ok().setData("item" , courseInfoForm);
    }
    //3、更新课程基本信息
    @PutMapping("update-course-info/{courseId}")
    //课程基本信息包含 course表和 course_description表中的数据
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable String courseId){
        courseService.updateCourseInfo(courseInfoForm,courseId);
        return R.ok();
    }
    //1、保存课程基本信息
    @PostMapping("save-course-info")
    //课程基本信息包含 course表和 course_description表中的数据
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().setData("id" , courseId);//返回课程id
    }

}

